// pages/meeting-room/book/home/index.js
import moment from '../utils/moment'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    initScrollTop: 0,
    reserveList: {},
    ruleShow: true,
    weekText: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
    selected: '',
    selectedRoom: '',
    selectedTime: '',
    month: '',
    today: '',
    dateList: [],
    timeList: [
      '00:00', '00:30', '01:00', '01:30', '02:00', '02:30',
      '03:00', '03:30', '04:00', '04:30', '05:00', '05:30',
      '06:00', '06:30', '07:00', '07:30', '08:00', '08:30',
      '09:00', '09:30', '10:00', '10:30', '11:00', '11:30',
      '12:00', '12:30', '13:00', '13:30', '14:00', '14:30',
      '15:00', '15:30', '16:00', '16:30', '17:00', '17:30',
      '18:00', '18:30', '19:00', '19:30', '20:00', '20:30',
      '21:00', '21:30', '22:00', '22:30', '23:00', '23:30',
    ],
    scrollFlag: 0,
    scrollTop: 0,
    scrollLeft: 0,
    roomList: [],
    curDateIndex: 0,
    bottomId: null, //跳转
    timeDivision: '' // 当前时分
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    setTimeout(() => {
      this.setData({
        ruleShow: false
      })
    }, 3000)
    this.getList()
    this.initDate()
    // this.getReserveList()
    setTimeout(() => {
      this.scrollBottom()
    }, 500)
  },
  // 获取预定情况列表
  getReserveList() {
    let list = [{
      "id": "1632936018460131329",
      "roomName": "测试会议室3",
      "reserveDate": "2023-03-07",
      "reserveStartTime": "11:00",
      "reserveEndTime": "11:30",
      "reservationBy": "李圣",
      "status": "1"
    }, {
      "id": "1632924328716709889",
      "roomName": "测试会议室",
      "reserveDate": "2023-03-07",
      "reserveStartTime": "11:00",
      "reserveEndTime": "11:30",
      "reservationBy": "李圣",
      "status": "1"
    }, {
      "id": "1632924136428843009",
      "roomName": "测试会议室2",
      "reserveDate": "2023-03-07",
      "reserveStartTime": "11:00",
      "reserveEndTime": "11:30",
      "reservationBy": "李圣",
      "status": "2"
    }];
    let obj = {};
    list.forEach(item => {
      if (!obj[item.roomName]) {
        obj[item.roomName] = {}
      }
      let startHour = parseInt(item.reserveStartTime.split(':')[0]),
        startMinute = parseInt(item.reserveStartTime.split(':')[1])
      let endHour = parseInt(item.reserveEndTime.split(':')[0]),
        endMinute = parseInt(item.reserveEndTime.split(':')[1])
      let count = ((endHour - startHour) * 2 * 30 + endMinute - startMinute) / 30;
      for (let i = 0; i < count; i++) {
        obj[item.roomName][this.data.timeList.indexOf(item.reserveStartTime) + i] = item
      }
    })
    this.setData({
      reserveList: obj
    })
  },
  // 初始化日期
  initDate() {
    let DATE = new Date(),
      year = DATE.getFullYear(),
      month = DATE.getMonth() + 1,
      date = DATE.getDate(),
      hour = DATE.getHours()
    this.setData({
      today: year + '-' + this.zero(month) + '-' + this.zero(date),
      selected: year + '-' + this.zero(month) + '-' + this.zero(date),
      month: month,
    })
    this.createDays();
  },
  // 获取会议室名称
  getList() {
    this.setData({
      roomList: [{
        "id": "1628667087272546305",
        "name": "测试会议室",
        "location": "会议室二楼",
        "verify": "1",
        "enable": "1"
      }, {
        "id": "1628667964075020290",
        "name": "测试会议室2",
        "location": "测试地点",
        "verify": "0",
        "enable": "1"
      }, {
        "id": "1628936288210001922",
        "name": "测试会议室3",
        "location": "3楼b101",
        "verify": "1",
        "enable": "1"
      }]
    })
  },
  onRuleClose() {
    this.setData({
      ruleShow: false
    })
  },
  //补全0
  zero: function (i) {
    return i >= 10 ? i : '0' + i;
  },
  createDays() {
    let time = moment(new Date()).startOf('week')
    let dateList = [];
    for (let j = 0; j < 2; j++) {
      time = time.add(j, 'w')
      let tempList = [];
      for (let i = 0; i < 7; i++) {
        let tempTime = time.weekday(i)
        tempList.push({
          date: tempTime.date(),
          dateFormat: this.zero(tempTime.date()),
          month: tempTime.month() + 1,
          monthFormat: this.zero(tempTime.month() + 1),
          year: tempTime.year(),
          week: this.data.weekText[tempTime.weekday()]
        });
      }
      dateList.push(tempList)
    }
    this.setData({
      dateList
    })
  },
  // 切换日期
  onDateSelected({
    currentTarget
  }) {
    let temp = currentTarget.dataset.date
    this.setData({
      selected: temp.year + '-' + temp.monthFormat + '-' + temp.dateFormat
    })
    this.getReserveList()
    setTimeout(() => {
      this.scrollBottom()
    }, 500)
  },
  // 滑动右侧会议室预定格子，左边时间跟着滚动
  updateAreaScroll(detail) {
    this.setData({
      scrollTop: detail.scrollTop,
      scrollLeft: detail.scrollLeft
    })
  },
  // 禁止滑动
  catchTouchMove() {
    return false;
  },
  // 点击今日、月份获取会议室预定情况列表
  selectToday() {
    this.setData({
      curDateIndex: 0,
      selected: this.data.today
    })
    this.getReserveList()
  },
  // 预定会议室
  handleOrder(e) {
    let selected = {}
    selected.reserve = null;
    selected.date = this.data.selected;
    selected.room = e.currentTarget.dataset.room;
    selected.time = e.currentTarget.dataset.time;
    if (this.data.reserveList[selected.room.name] && this.data.reserveList[selected.room.name][this.data.timeList.indexOf(selected.time).toString()]) {
      selected.reserve = this.data.reserveList[selected.room.name][this.data.timeList.indexOf(selected.time).toString()]
    }
    wx.navigateTo({
      url: '/pages/meeting-room/book/add/index?selected=' + JSON.stringify(selected) + '&flag=1',
    })
  },
  // 滚动到指定位置
  scrollBottom() {
    let DATE = new Date(),
      hour = DATE.getHours(),
      minutes = DATE.getMinutes()
    if (minutes >= 30) {
      minutes = '30'
    } else {
      minutes = '00'
    }
    this.setData({
      timeDivision: `${this.zero(hour)}:${minutes}`
    })
    const index = this.data.timeList.indexOf(this.data.timeDivision);
    this.setData({
      bottomId: `scroll${index}`,
    })
  },
  onClickMore() {},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getReserveList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
})